# satellite_calculations.py
import math

def getData(latitude, longitude, satellite_longitude):
    # 放置完整的计算逻辑
    """
        计算水平角、俯仰角和高频头极化角
        :param latitude: 当前所在地纬度 (degrees)
        :param longitude: 当前所在地经度 (degrees)
        :param satellite_longitude: 卫星经度 (degrees)
        :return: (azimuth, elevation, lnb_skew)
        """
    # 将角度转换为弧度
    lat_rad = math.radians(latitude)
    lon_rad = math.radians(longitude)
    sat_lon_rad = math.radians(satellite_longitude)

    # 计算 ΔL (卫星经度 - 当前所在地经度)
    delta_lon = sat_lon_rad - lon_rad

    # 获取卫星锅方位角
    azimuth = getAzimuth(delta_lon, lat_rad)
    elevation = getElevation(delta_lon, lat_rad)
    lnbskew = getLNBSkew(delta_lon, lat_rad)
    return azimuth, elevation, lnbskew

def getAzimuth(longitudeDiff,latitude):
    # 获取方位角度
    mAzimuth = math.atan2(math.tan(longitudeDiff) / math.sin(latitude), -1)  # 计算方位角的弧度
    mAzimuth = math.degrees(mAzimuth)  # 将计算好的弧度转化为度数
    mAzimuth = (mAzimuth + 360) % 360  # 将度数限制在[0, 360] 范围
    return mAzimuth

def getElevation(longitudeDiff,latitude):
    # 地球和卫星的几何参数
    earth_radius = 6378 # 地球半径，单位：km
    geostationary_altitude = 35786  # 地球静止轨道高度，单位：km
    # 获取俯仰角度
    cosr = math.cos(latitude)*math.cos(longitudeDiff)*earth_radius/(earth_radius+geostationary_altitude)
    mElevation = math.atan((math.cos(latitude)*math.cos(longitudeDiff) - cosr)/math.sqrt(1-math.pow(math.cos(latitude)*math.cos(longitudeDiff),2))) # 计算俯仰角弧度
    mElevation = math.degrees(mElevation) # 将计算好的弧度转化为度数
    return mElevation

def getLNBSkew(longitudeDiff,latitude):
    # 获取高频头的极化角度
    mLNBSkew = math.atan(math.sin(longitudeDiff)/math.tan(latitude))
    mLNBSkew = math.degrees(mLNBSkew)  # 将计算好的弧度转化为度数
    return mLNBSkew
